package controllers

import (
	"encoding/json"
	"exam4_9/models"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Create() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	title := c.GetString("title")
	data := c.GetString("data")
	var news models.News
	news.Title = title
	news.Data = data
	if err := news.Create(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	news.GetNewsByTitle()
	key := fmt.Sprintf("news:%d", news.ID)
	jsonData, _ := json.Marshal(news)
	if err := models.Rdb.Set(models.Ctx, key, jsonData, -1).Err(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
	return
}
func (c *NewsController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	var news models.News
	news.ID = uint(id)
	if err := news.Del(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("news:%d", news.ID)
	models.Rdb.Del(models.Ctx, key)
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *NewsController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	title := c.GetString("title")
	data := c.GetString("data")
	id, _ := c.GetInt("id")
	var news models.News
	news.Title = title
	news.Data = data
	news.ID = uint(id)
	if err := news.Updates(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	news.GetNewsByTitle()
	key := fmt.Sprintf("news:%d", news.ID)
	jsonData, _ := json.Marshal(news)
	models.Rdb.Set(models.Ctx, key, jsonData, -1)
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "修改成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *NewsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var Newses []models.News
	keys, _ := models.Rdb.Keys(models.Ctx, "news:*").Result()
	if len(keys) == 0 {
		models.DB.Find(&Newses)
		for _, news := range Newses {
			key := fmt.Sprintf("news:%d", news.ID)
			jsonData, _ := json.Marshal(news)
			models.Rdb.Set(models.Ctx, key, jsonData, -1)
		}
	}
	for _, key := range keys {
		var news models.News
		jsondata, _ := models.Rdb.Get(models.Ctx, key).Result()
		json.Unmarshal([]byte(jsondata), &news)
		Newses = append(Newses, news)
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: Newses,
	}
	c.ServeJSON()
}
